// 
// Decompiled by Procyon v0.5.30
// 

package com.baturu.vin.benz.dto.footnote;

import com.baturu.vin.benz.dto.BenzVinInfoDTO;
import com.google.common.collect.Maps;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class FootnoteList {
    private List<Footnote> fnList;
    private boolean hasCkd;
    private String plants;

    public FootnoteList() {
        this(10);
    }

    public FootnoteList(int size) {
        this.hasCkd = false;
        this.fnList = new ArrayList<>(size);
    }

    public boolean isHasCkd() {
        return hasCkd;
    }

    public void setHasCkd(boolean hasCkd) {
        this.hasCkd = hasCkd;
    }

    public boolean isEmpty() {
        return this.fnList.isEmpty();
    }

    public int size() {
        return this.fnList.size();
    }

    public Footnote get(int x) {
        return this.fnList.get(x);
    }

    public List<Footnote> getAll() {
        return this.fnList;
    }

    public boolean add(FootnoteList fnl) {
        int i = 0;
        boolean returnValue = true;
        if (fnl == null) {
            return true;
        }
        for (i = 0; i < fnl.size(); ++i) {
            if (this.add(fnl.get(i)) < 0) {
                returnValue = false;
            }
        }
        return returnValue;
    }

    public int add(Footnote fn) {
        return this.add(fn, false);
    }

    private void addAll(List<Footnote> collect) {
        collect.forEach(this::add);
    }

    private int add(Footnote fn, boolean associatedNote) {
        if (fn == null) {
            return -1;
        }
        String addToFootnumber = fn.getFNoteNum();
        int x = this.fnList.size() - 1;
        for (;x >= 0 && this.fnList.get(x).getFNoteNum().compareTo(addToFootnumber) > 0; --x) {
        }
        fn.setIsAssociated(associatedNote);
        this.fnList.add(x + 1, fn);
        return x + 1;
    }

    public void remove(Footnote fn) {
        this.fnList.remove(fn);
    }

    public void removeAll(List<Footnote> footnotes) {
        this.fnList.removeAll(footnotes);
    }

    public FootnoteList getFilteredFootNoteList(FootnoteList pFootnoteList, BenzVinInfoDTO benzVinInfoDTO, int statusCKD) {
        boolean addAllOfTheseNotes = false;
        boolean addedPart = false;
        int x = 0;
        int previousIndex = 0;
        int footnoteValidReturn = 0;
        String previousFN = "UNIQUE NUMBER";
        String currentFN = "SOMETHINGDIFFERENT";
        FootnoteList returnResult = null;
        returnResult = new FootnoteList(this.fnList.size());
        returnResult.setPlants(this.plants);
        if (this.fnList.size() <= 0) {
            return returnResult;
        }
        for (x = 0; x < this.fnList.size(); ++x) {
            Footnote aFootnote = this.fnList.get(x);
            if (aFootnote != null) {
                currentFN = aFootnote.getFNoteNum();
                if (!previousFN.equals(currentFN)) {
                    addAllOfTheseNotes = false;
                    if (x > 0 && !addedPart) {
                        if ((this.fnList.get(x - 1) instanceof FootnoteRev9Color ||
                                this.fnList.get(x - 1) instanceof FootnoteRev8Color)) {
                            x = previousIndex - 1;
                            addAllOfTheseNotes = true;
                            continue;
                        }
                    }
                    previousFN = currentFN;
                    previousIndex = x;
                    addedPart = false;
                }
                if (!addAllOfTheseNotes) {
                    footnoteValidReturn = aFootnote.isValid(pFootnoteList, benzVinInfoDTO, statusCKD);
                }
                if (addAllOfTheseNotes || aFootnote.isSerial || footnoteValidReturn == 0 || (aFootnote.isColor && footnoteValidReturn == 9)) {
                    returnResult.add(aFootnote);
                    addedPart = true;
                } else if (x > 0 && x == this.fnList.size() - 1 && !addedPart && (this.fnList.get(x - 1) instanceof FootnoteRev9Color || this.fnList.get(x - 1) instanceof FootnoteRev8Color)) {
                    x = previousIndex - 1;
                    addAllOfTheseNotes = true;
                }
            }
        }
        return returnResult;
    }


    public FootnoteList getFootnotes(String footnoteNumbers) {
        FootnoteList fnl = new FootnoteList();
        fnl.setPlants(this.plants);
        return this.getFootnotes(fnl, footnoteNumbers);
    }

    public FootnoteList getSerialFootnotes() {
        FootnoteList returnResult = new FootnoteList(fnList.size());
        returnResult.setPlants(plants);
        returnResult.addAll(fnList.stream().filter(Footnote::isSerial).collect(Collectors.toList()));
        return returnResult;
    }


    private FootnoteList getFootnotes(FootnoteList returnFnl, String footnoteNumbers) {
        int counter = 0;
        int initialSize = 0;
        int fnlSize = 0;
        Map<String, Footnote> temp = Maps.newHashMap();
        Map<String, String> footnoteNumbersHash = Maps.newHashMap();
        Footnote oneFootnote;
        if (returnFnl == null) {
            returnFnl = new FootnoteList();
            returnFnl.setPlants(plants);
        } else {
            initialSize = returnFnl.size();
        }
        if (footnoteNumbers == null || footnoteNumbers.length() == 0) {
            return returnFnl;
        }
        for (int i = 0; i < footnoteNumbers.length(); i += 3) {
            if (i + 3 > footnoteNumbers.length()) {
                footnoteNumbersHash.put(footnoteNumbers.substring(i), "");
            } else {
                footnoteNumbersHash.put(footnoteNumbers.substring(i, i + 3), "");
            }
        }
        for (fnlSize = this.fnList.size(), counter = 0; counter < fnlSize; ++counter) {
            oneFootnote = this.fnList.get(counter);
            if (oneFootnote != null) {
                if (footnoteNumbersHash.containsKey(oneFootnote.getFNoteNum())) {
                    returnFnl.add(oneFootnote, false);
                }
            }
        }
        if (returnFnl.size() > 0 && returnFnl.get(0) instanceof FootnoteRev8) {
            for (fnlSize = returnFnl.size(), counter = initialSize; counter < fnlSize; ++counter) {
                temp.put(returnFnl.get(counter).getFNoteNum(), returnFnl.get(counter));
            }
            String prevListNum = "-999";
            String currListNum = "";
            for (Map.Entry<String, Footnote> t : temp.entrySet()) {
                Footnote tempF = t.getValue();
                currListNum = tempF.getListNum();
                if ("-999".equals(prevListNum)) {
                    returnFnl = this.getFootnotesByList(returnFnl, tempF, footnoteNumbersHash);
                } else if (!prevListNum.equals(currListNum)) {
                    returnFnl = this.getFootnotesByList(returnFnl, tempF, footnoteNumbersHash);
                }
                prevListNum = currListNum;
            }
        }
        return returnFnl;
    }


    private FootnoteList getFootnotesByList(FootnoteList returnFnl, Footnote pFootnote, Map footnoteNumbersHash) {
        int x = 0;
        int fnlSize = 0;
        String listNum1 = pFootnote.getListNum();
        if (listNum1.length() <= 0) {
            return returnFnl;
        }
        if (returnFnl == null) {
            returnFnl = new FootnoteList();
            returnFnl.setPlants(this.plants);
        }
        for (fnlSize = this.fnList.size(), x = 0; x < fnlSize; ++x) {
            Footnote footnote = this.fnList.get(x);
            String listNum = footnote.getListNum();
            String fNoteNum = footnote.getFNoteNum();
            if (listNum.equals(listNum1) && !fNoteNum.equals(pFootnote.getFNoteNum()) && !footnoteNumbersHash.containsKey(fNoteNum)) {
                returnFnl.add(footnote, true);
            }
        }
        return returnFnl;
    }

    public FootnoteList getFootnotesByType(Class classOrType) {
        FootnoteList rV = new FootnoteList();
        rV.setPlants(this.plants);
        List<Footnote> collect = this.fnList.stream().filter(oneFn -> oneFn.getClass().equals(classOrType)).collect(Collectors.toList());
        rV.addAll(collect);
        return rV;
    }


    public String getPlants() {
        return this.plants;
    }

    public void setPlants(String plants) {
        this.plants = plants;
    }

    public boolean nonUniqueES2(FootnoteList colorfnl) {
        if (colorfnl.size() == 0) {
            return false;
        }
        if (colorfnl.size() == 1) {
            return true;
        }
        Footnote twoFn = null;
        for (int x = 0; x < colorfnl.size(); ++x) {
            Footnote oneFn = colorfnl.get(x);
            String oneES2Codes = oneFn.getEs2Codes();
            for (int y = x + 1; y < colorfnl.size(); ++y) {
                twoFn = colorfnl.get(y);
                String twoES2Codes = twoFn.getEs2Codes();
                if (oneES2Codes != null && twoES2Codes != null && !oneES2Codes.equals(twoES2Codes)) {
                    return false;
                }
            }
        }
        return true;
    }
}
